#include "tree_2_4.h"
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

void ReadFile ( string fileName );

Tree_2_4 *tree = NULL;


int main ( )
{
	string command;
	
	while ( true )
	{
		cin >> command;

		if ( command == "initialize" )
		{
			tree = new Tree_2_4();
			cout << "Tree initialized" << endl;
		}
		else if ( command == "insert" )
		{
			int studentId, grade;
			string courseName, courseId;

			cin >> studentId;
			cin >> courseId;
			cin >> courseName;
			cin >> grade;

			if ( tree == NULL)
			{
				cout << "You must initialize the tree. Type \"help\" for help" << endl;
			}
			else	
			{
				tree->Insert ( studentId, courseId, (char *)courseName.c_str(), grade );
			}
		}
		else if ( command == "load" )
		{
			string fileName;
			cin >> fileName;
		
			ReadFile ( fileName );
		}
		else if ( command == "lookup" )
		{
			int studentId;

			cin >> studentId;
			tree->PrintLessons ( studentId );
		}
		else if ( command == "rlookup" )
		{
			int from, to;

			cin >> from;
			cin >> to;

			tree->RangeFind ( from, to );
		}
		else if ( command == "delete" )
		{
			int studentId;
			string courseId;

			cin >> studentId;
			cin >> courseId;

			tree->Delete (studentId, courseId );
		}
		else if ( command == "sdelete" )
		{
			int studentId;

			cin >> studentId;

			tree->Delete ( studentId );
		}
		else if ( command == "exit" )
		{
			delete tree;
			exit ( 1 );
		}
		else if ( command == "help")
		{
			cout << "initialize: initializes the tree" << endl
				 << "insert: inserts given data to the tree" << endl
				 << "load: executes the commands written in the given file" << endl
				 << "lookup: prints the lesson information of the given student" << endl
				 << "rlookup: prints the lesson information of the students with id between the two given values" << endl
				 << "delete: deletes the lesson with id equal to the given from the student with id equal to the given, if exists" << endl
				 << "sdelete: delete the student with id equal to the given" << endl
				 << "exit: destoys the tree" << endl;
		}
		else
		{
			cout << "Type \"help\" for help" << endl;
		}

	}

	return 0;
}






void ReadFile ( string fileName )
{
	string command;
	fstream input ( fileName.c_str(), fstream::in );

	while ( !input.eof() )
	{
		input >> command;

		if ( command == "initialize" )
		{
			tree = new Tree_2_4();
			cout << "Tree initialized" << endl;
		}
		else if ( command == "insert" )
		{
			int studentId, grade;
			string courseName, courseId;

			input >> studentId;
			input >> courseId;
			input >> courseName;
			input >> grade;

			if ( tree == NULL)
			{
				cout << "You must initialize the tree. Type \"help\" for help" << endl;
			}
			else
			{
				tree->Insert ( studentId, courseId, (char *)courseName.c_str(), grade );
			}
		}
		else if ( command == "lookup" )
		{
			int studentId;

			input >> studentId;
			tree->PrintLessons ( studentId );
		}
		else if ( command == "rlookup" )
		{
			int from, to;

			input >> from;
			input >> to;

			tree->RangeFind ( from, -to );
		}
		else if ( command == "delete" )
		{
			int studentId;
			string courseId;

			input >> studentId;
			input >> courseId;

			tree->Delete (studentId, courseId );
			cout << "Deleted " << studentId << " " << courseId << " " << endl;
		}
		else if ( command == "sdelete" )
		{
			int studentId;

			input >> studentId;

			tree->Delete ( studentId );
			cout << "Deleted " << studentId << endl;
		}
	}
}

